const Koa = require('koa')
const app = new Koa()
const bodyParser = require('koa-bodyparser')
app.use(bodyParser())
const router = require('./routes')
// app.use((ctx, next) => {
// let data = await query('SELECT * FROM users')
//   ctx.body = 'hello world'
//   next()
// })

// router.get('/aa', async (ctx) => {
//   let data = await User.findAll({
//     attributes: ['id', 'name'],
//   })
//   ctx.body = {
//     data: data,
//   }
// })

app.use(router.routes()) // 启用路由
app.use(router.allowedMethods())

app.use(async (ctx, next) => {
  ctx.body = '404!~'
  next()
})

var server = app.listen(3000, 'localhost', function () {
  var host = server.address().address
  var port = server.address().port
  console.log('koa服务已启动, http://%s:%s', host, port)
})
